--[[
Ok 1st we need to establish the connection with MakeConnection which will return the index that we can access the database with.
Note the last argument isnt passed here.(Optional) It is a table of settings to override the config.lua default settings. Look there for the configs. 
]]
local DB = gdatabase.MakeConnection("192.168.1.1", "someusername", "somepassword", "somedatabasename")

--[[
With the connection we made above, GDatabase will automatically start pinging the mysql server to make sure its still up
If the connection was to suddenly go down, once GDatabase detects this it will fire off its disconnect hook, so we can do cleanup
All the hooks that you can use are in the README.html, so please read that.
]]
function cleanupondisconnect()
	timer.Remove("sometimer") 
end
gdatabase.HookAdd("shutdown", "cleanupondisconnect", cleanupondisconnect, DB) --This hooks the above function to the GDatabase disconnect hook.

--As with the above we can also have gdatabase keep track of the timers for us by creating the timer with the gdatabase.AddTimer
function helloworld()
	local query = gdatabase.Query("SELECT * FROM users;", DB) --This line does a standard query(non-threaded meaning the gmod server waits for the query) and returns the query wrapped around a table
	PrintTable(query)
end

--gdatabase.AddTimer(strID, intInterval, intRep, intDatabaseIndex, fnCallback, ...)
gdatabase.AddTimer("helloworldtimer", 10, 0, DB, helloworld)
--[[
Now if the database was to go down, GDatabase will automatically stop(pause actually) the timer. When it comes back up it will start it back up.
If the database goes down for good with a gdatabase.KillConnection or console command then it will remove the timers associated with the database.
]]

--We can also manually check for a connection with gdatabase.CheckForConnection
local isRunning = gdatabase.CheckForConnection(DB)

if !isRunning then
	print("OH NO!!!")
else
	print("YAY!!!")
end

--[[
A couple of things about threaded queries. How they work is when you make a threaded query, the query isnt ran instantly. Instead it add's it to a queue and run's in a First-Come First Served based on
how many concurrent threads you have told gdatabase to run at(defaults to 2). If for some reason the queue gets full(defaults 25) it will return false to let you know the query wasn't added. 

Gdatabase will also check to see if a thread is taking to long to run(10 seconds timeout by default) if it is it will purge the thread.

Also note that all threaded queries are based on the built-in timer module. This means that threaded queries WILL NOT run until the 1st connection attempt, this is a gmod design and can't be changed.
]]

function PrintData(query, strTitleText)
	if (query == 0) then
		print("QUERY FAILED!!!")
		return
	end
	
	print(strTitleText)
	PrintTable(query)
end

--gdatabase.ThreadedQuery(strQuery, intDatabaseIndex, fnCallback, ...)
gdatabase.ThreadedQuery("SELECT 1+1 FROM servers", DB, PrintData, "SOME TITLE")
--After the thread is ran, GDatabase will call the function you pass it. If no function is passed it will just call a general callback that does nothing.



